package com.sss.gsb.ws.enpoints;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ws.server.endpoint.annotation.Endpoint;
import org.springframework.ws.server.endpoint.annotation.PayloadRoot;

import com.sss.gsb.message.schemas.DeadmanRequest;
import com.sss.gsb.message.schemas.DeadmanResponse;
import com.sss.gsb.orm.model.DeadMan;
import com.sss.gsb.service.LoadParameter;
import com.sss.gsb.service.impl.DeadManService;
import com.sss.gsb.util.SchemaConvertionUtils;

@Endpoint
public class CPKDeadManEndpoint implements CPKEndpoint {
	private static final Logger log = LoggerFactory.getLogger(CPKDeadManEndpoint.class);
	
	@Autowired
	private DeadManService service;
	
	@PayloadRoot(localPart="DeadmanRequest",namespace=NAMESPACE)
	public DeadmanResponse getDeadman(DeadmanRequest request){
		log.info("Loading deadman endpoint...");
		try{
			LoadParameter param = SchemaConvertionUtils.toLoadParameter(request);
			List<DeadMan> result = this.service.loadData(param);
			DeadmanResponse response = new DeadmanResponse();
			for(DeadMan dm : result){
				response.getDeadman().add(SchemaConvertionUtils.toSchema(dm));
			}
			return response; 
		}
		catch(Exception e){
			log.error("[ Could not load data from endpoint :: ["+e.getMessage()+"]",e);
		}
		return null;
	}
}
